package com.atguigu.tingshu.user.strategy.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.strategy.PaidRecordStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author: atguigu
 * @create: 2024-08-23 10:32
 */
@Slf4j
@Component("1001")
public class AlbumPaidRecordStrategy implements PaidRecordStrategy {

    @Autowired
    private UserPaidAlbumMapper userPaidAlbumMapper;


    /**
     * 处理购买商品类型为专辑-发货逻辑
     * @param userPaidRecordVo
     */
    @Override
    public void handlerPaidRecord(UserPaidRecordVo userPaidRecordVo) {
        log.info("[用户服务]处理购买商品类型为专辑发货逻辑");
        //2.TODO 处理购买项目类型-专辑
        //2.1 根据订单编号查询是否处理过专辑购买记录
        LambdaQueryWrapper<UserPaidAlbum> paidAlbumLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paidAlbumLambdaQueryWrapper.eq(UserPaidAlbum::getOrderNo, userPaidRecordVo.getOrderNo());
        if (userPaidAlbumMapper.selectCount(paidAlbumLambdaQueryWrapper) > 0) {
            return;
        }
        //2.2 保存专辑购买记录
        if (CollectionUtil.isNotEmpty(userPaidRecordVo.getItemIdList())) {
            Long albumId = userPaidRecordVo.getItemIdList().get(0);
            UserPaidAlbum userPaidAlbum = new UserPaidAlbum();
            userPaidAlbum.setOrderNo(userPaidRecordVo.getOrderNo());
            userPaidAlbum.setUserId(userPaidRecordVo.getUserId());
            userPaidAlbum.setAlbumId(albumId);
            userPaidAlbumMapper.insert(userPaidAlbum);
        }
    }
}
